<?php
/**
 * 商圈模型
 */

namespace app\official\model;


use app\common\model\BaseModel;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class BusinessArea extends BaseModel {

    /**
     * 市或者区下面的所有商圈
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params) {
        $column = ['name', 'city_code', 'id', 'x(location) as lng', 'Y(location) as lat'];
        //区域
        $per = db::name('region')
            ->where('p_code', $params['city_code'])
            ->order ('weights','asc')
            ->field($column)
            ->select();
        $ids = array_column($per, 'city_code');
        //区域下面所有商圈
        $business_area = db::name('business_area')
            ->where('city_id', 'in', $ids)
            ->field('name,city_id,id,x(location) as lng,Y(location) as lat')
            ->select();
        $per = array_column($per, null, 'city_code');
        foreach ($business_area as $val) {
            if (isset($per[$val['city_id']])) {
                $per[$val['city_id']]['children'][] = $val;
            }
        }
        $per = array_values($per);
        return $per;
    }

    /**
     * 商圈树结构
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList2($params) {
        $column = ['name', 'city_code', 'id'];
        //区域
        $rank3 = db::name('region')
            ->where('p_code', $params['city_code'])
            ->order ('weights','asc')
            ->field($column)
            ->select();
        $ids = array_column($rank3, 'city_code');
        //区域下面所有商圈
        $business_area = db::name('business_area')
            ->where('city_id', 'in', $ids)
            ->field('name,city_id,x(location) as lng,Y(location) as lat')
            ->select();
        $rank3 = array_column($rank3, null, 'city_code');
        foreach ($business_area as $val) {
            if (isset($rank3[$val['city_id']])) {
                $rank3[$val['city_id']]['children'][] = $val;
            }
        }
        $rank3 = array_values($rank3);
        foreach ($rank3 as $k => $v) {
            unset($rank3[$k]['city_code']);
            unset($rank3[$k]['id']);
            if (!empty($rank3[$k]['children'])) {
                foreach ($rank3[$k]['children'] as $k1 => $v1) {
                    $rank3[$k]['children'][$k1]['lng'] = (string)$v['children'][$k1]['lng'];
                    $rank3[$k]['children'][$k1]['lat'] = (string)$v['children'][$k1]['lat'];
                    unset($rank3[$k]['children'][$k1]['city_id']);
                }
            } else {
                unset($rank3[$k]);
            }
        }
        return array_values($rank3);
    }

    /**
     * 市下面的区是否有商圈
     * @param $params
     * @return int
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function has_data($params) {
        $rank3 = db::name('region')
            ->where('p_code', $params['city_code'])
            ->select();
        $ids = array_column($rank3, 'city_code');
        //区域下面所有商圈
        $business_area = db::name('business_area')
            ->where('city_id', 'in', $ids)
            ->select();
        if ($business_area) {
            return 1;
        } else {
            return 2;
        }
    }

}
